package com.greate.community.controller.master;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.greate.community.dao.admin.RBACMapper;
import com.greate.community.dao.admin.UserAdminMapper;
import com.greate.community.entity.Role;
import com.greate.community.entity.admin_master.UserAdmin;
import com.greate.community.service.master.RBACService;
import com.greate.community.service.master.UserAdminService;
import com.greate.community.util.CommunityUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * 管理员管理用户的接口
 */
@RestController
@RequestMapping("/master")
public class UserAdminController {

    @Autowired
    UserAdminService userAdminService;
    @Autowired
    RBACService rbacService;
    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/userInfo")
    public List<UserAdmin> getUserInfo(){
        List<UserAdmin> userAdmins = userAdminService.queryUserInfo();
        System.out.println(userAdmins);

        return userAdmins;
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @RequestMapping("/delete/{userId}")
    public String deleteUser(@PathVariable("userId") int userId){
       return userAdminService.deleteUserById(userId);
    }

    /**
     * 上传更改用户头像
     * @param file
     * @param userId
     * @return
     */
    @PostMapping("/uploadHeader")
    public String uploadHeader(@RequestParam("file") MultipartFile file,@RequestParam("userId") Integer userId){
        System.out.println("上传测试"+userId);
        if (userId!=null){
            return  userAdminService.uploadHeader(file,userId);
        }
        return CommunityUtil.getJSONString(500,"参数错误");

    }

    /**
     * 获得所有角色信息
     * @return
     */
    @RequestMapping("/allRoles")
    public List<Role> getAllRoles(){
        return rbacService.selectAllRoles();
    }

    /**
     * 修改用户信息
     * @return
     */
    @PostMapping("/updateUserInfo")
    public String updateUserInfo(@RequestBody Map<String,String> UserInfo){
        System.out.println(UserInfo);
        return userAdminService.undateUserInfo(UserInfo);
    }

    /**
     * 添加用户
     * @return
     */
    @PostMapping("/addUserInfo")
    public String addUserInfo(@RequestBody Map<String, String> user){
        System.out.println(user);
        return userAdminService.addUser(user);
    }
}
